package com.mystaggeredview.base;

import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;

import butterknife.ButterKnife;

/**
 * @Auth Mr.lc(兰超)
 * @Date 2018/4/25
 * @Time 10:10
 */

public abstract class BaseActivity extends AppCompatActivity {
    public String TAG ;
    public Context mContext;
    public static final String EXITACTION = "action.exit";

    private ProgressDialog loadDialog;
    private ExitReceiver exitReceiver = new ExitReceiver();
    @Override
    public void onCreate(Bundle savedInstanceState) {

//        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);   /**继承AppCompatActivity中使用真的是深坑啊*/
        super.onCreate(savedInstanceState);
        setContentView(getResourcesId());
        mContext = getApplicationContext();
        ButterKnife.bind(this);
        TAG = getRunningActivityName();
        init(savedInstanceState);
        exitInit();
    }
    protected abstract int getResourcesId();
    protected abstract void init(Bundle savedInstanceState);
    private String getRunningActivityName(){
        String contextString = this.toString();
        return contextString.substring(contextString.lastIndexOf(".")+1, contextString.indexOf("@"));
    }


    private void exitInit(){
        IntentFilter filter = new IntentFilter();
        filter.addAction(EXITACTION);
        registerReceiver(exitReceiver, filter);
    }
    class ExitReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(exitReceiver);
        dismissDialog();
        loadDialog=null;
    }

    public void showLoadDialog(String message){
        if (loadDialog==null){
            loadDialog= new ProgressDialog(this);
            loadDialog.setMessage(message);
            loadDialog.show();
        } else if (!loadDialog.isShowing()){
            loadDialog.setMessage(message);
            loadDialog.show();
        }else {
            loadDialog.setMessage(message);
            loadDialog.show();
        }
        loadDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                if (i==KeyEvent.KEYCODE_BACK){
                    return true;
                }else return false;
            }
        });
    }

    public void showLoadDialog(){
        showLoadDialog("加载中");
    }
    public void dismissDialog(){
        if (loadDialog!=null&&loadDialog.isShowing()){
            loadDialog.dismiss();
        }
    }
    /**
     * 设置返回键事件
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            finish();
        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();

    }
}
